<template>
  <div class="header">
    <div>
      <router-link to="/blog">博客首页</router-link>
    </div>
    <div v-if="jurisdiction">
      <router-link to="/write">写博客</router-link>
    </div>
    <div class="aboutMe" @mouseenter="showAbout=true" @mouseleave="showAbout=false">
      关于我
      <transition name="about">
        <ul v-show="showAbout">
          <li>
            <a @click="jump('5f488981f923821c286e514f')">博主介绍</a>
          </li>
          <li>
            <router-link to="/resume">博主简历</router-link>
          </li>
        </ul>
      </transition>
    </div>
    <div>
      <router-link to="/case">效果案例</router-link>
    </div>
    <div>
      <router-link to="/messageBoard">留言</router-link>
    </div>
    <div class="loginBtn" v-if="!jurisdiction">
      <el-button type="info" size="mini" @click="showLoginVisible=true">管理员登录</el-button>
    </div>
    <div class="user" v-else @mouseenter="showLogout=true" @mouseleave="showLogout=false">
      <div class="username">{{username}}</div>
      <el-button class="logout" type="mini" v-if="showLogout" @click="logout">退出</el-button>
    </div>

    <transition name="fade">
      <div class="login" v-show="showLoginVisible">
        <div class="loginTitle">Login</div>

        <el-form :model="loginForm" :rules="loginRules" ref="loginForm">
          <el-form-item label="用户名" prop="username" class="username">
            <el-input type="text" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" class="password">
            <el-input type="password" v-model="loginForm.password"></el-input>
          </el-form-item>
          <div class="btn">
            <el-button round type="success" @click="submit()">登录</el-button>
          </div>
        </el-form>

        <div class="mask"></div>
        <div class="close" @click="close">×</div>
      </div>
    </transition>

  </div>
</template>

<script>
  import {mapGetters} from 'vuex'

  export default {
    name: "Header",
    data() {
      return {
        showLogout: false,
        showLoginVisible: false,
        showAbout: false,
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          password: [{required: true, message: '请输入密码', trigger: 'blur'}]
        },
        username: window.sessionStorage.getItem('username')
      }
    },
    methods: {
      close() {
        this.$refs.loginForm.resetFields()
        this.showLoginVisible = false
      },
      submit() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.$store.dispatch('getUser', this.loginForm)
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      logout() {
        window.sessionStorage.removeItem('username')
        location.reload()
      },
      jump(id){
        this.$store.dispatch('getSingleIntroduce', id)
        this.$router.push('/introduce')
      }
    },
    computed: {
      ...mapGetters(['jurisdiction']),
    }
  }
</script>

<style scoped lang="less">
  .header {
    width: 1000px;
    margin: auto;
    height: 50px;
    padding-right: 40px;

    div {
      float: left;
      line-height: 50px;
      margin-left: 80px;
      color: #d9abaf;
      font-weight: bold;
    }

    .loginBtn {
      float: right;
    }

    .user {
      float: right;
      position: relative;
      width: 50px;
      text-align: center;
      font-size: 16px;
      cursor: pointer;

      div {
        margin-left: 0;
        width: 100%;
      }

      .logout {
        position: absolute;
        bottom: -15px;
        left: 0;
        background: #3d3d3d;
        color: white;
      }
    }

    .aboutMe {
      color: #25a4bb;
      font-size: 15px;
      position: relative;
      cursor: pointer;

      ul {
        position: absolute;
        z-index: 100;
        top: 40px;
        left: 0;
        width: 100px;
        background: #303133;
        color: white;
        transform: translateX(-25%);
        border-radius: 10px;
        box-shadow: 0 1px 1px #fff inset;
        overflow: hidden;

        li {
          line-height: 20px;
          padding: 10px 20px;

          a {
            color: lightskyblue;
            font-size: 14px;
          }

          a:hover {
            color: #25a4bb;
          }
        }
      }
    }
  }

  .header .login {
    width: 500px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 100;
    margin: 0;
    padding: 20px 0;

    div {
      float: none;
      width: 100%;
      margin: 5px 0 0 0;
      color: white;

      .el-input {
        background: none;
        border-radius: 5px;
        border: 1px solid #fff;
        height: 30px;
        width: 200px;
        color: white;
        padding: 0;
        position: relative;
        vertical-align: middle;
      }
    }

    .el-form-item {
      text-align: center;
    }

    label {
      margin-left: 50px;
    }

    .loginTitle {
      font-size: 20px;
      text-align: center;
    }

    .btn {
      text-align: center;
      margin-top: 10px;

      .el-button {
        display: inline-block;
        width: 120px;
        border-radius: 10px;
        color: white;
      }
    }

    .mask {
      width: 100%;
      height: 100%;
      background: #000;
      position: absolute;
      left: 0;
      top: 0;
      z-index: -1;
      opacity: .7;
      border-radius: 10px;
    }

    .close {
      color: white;
      width: 20px;
      height: 20px;
      line-height: 20px;
      text-align: center;
      background: #000;
      position: absolute;
      top: 0;
      right: 0;
      cursor: pointer;
      font-size: 16px;
      border-radius: 50%;
    }
  }

  .fade-enter-active, .fade-leave-active {
    transition: opacity .5s
  }

  .fade-enter, .fade-leave-active {
    opacity: 0
  }

  .about-enter-active, .about-leave-active {
    transition: height .5s linear;
  }

  .about-enter, .about-leave-to {
    height: 0;
  }
  .about-leave,.about-enter-to{
    height: 80px;
  }

</style>
